package com.xc.sbdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xc.sbdemo.entity.Fruit;
import com.xc.sbdemo.mapper.FruitMapper;
import com.xc.sbdemo.service.FruitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FruitServiceImpl implements FruitService {

    @Autowired
    private FruitMapper fruitMapper;

    @Override
    public List<Fruit> list() {
        return fruitMapper.selectList(null);
    }

    @Cacheable(value = "fruit",key = "#id")
    @Override
    public Fruit selectById(int id) {
        return fruitMapper.selectById(id);
    }



    @Override
    public int delete(int id) {
        return fruitMapper.deleteById(id);
    }

    @Override
    public int add(Fruit fruit) {
        return fruitMapper.insert(fruit);
    }

    // @CachePut(value = "fruit",key = "#fruit.fruitId")
    @CacheEvict(value = "fruit",key = "#fruit.fruitId")
    @Override
    public int update(Fruit fruit) {
        return fruitMapper.updateById(fruit);
    }

    @Override
    public Fruit selectByType(String type) {
        QueryWrapper<Fruit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",type);
        return fruitMapper.selectOne(queryWrapper);
    }


}
